package Part2_java.util.Map.TreeMap;
import java.util.Objects;

/**
 * ClassName: User
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.14 10:04
 */


class User implements Comparable{
    private int age;
    private String name;

    public User(){
        //创建对象时，初始化10行代码
    }

    public User(String name){
        this();//调用第一个空参构造器
        this.name=name;
    }

    public User(String name, int age){
        this(name);//调用第二个构造器
        //this.name=name;
        this.age=age;
    }

    public void setName(String name){
        //方法形参与类的属性重名，使用this标注属性，没有使用this的为形参
        this.name=name;//this：当前调用对象的属性
    }
    public void setAge(int age){
        this.age=age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("调用了equals方法");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        System.out.println("调用了hashCode方法");
        return Objects.hash(age, name);
    }

    @Override
    public int compareTo(Object o) {
        if (this==o){
            return 0;
        }
        if(o instanceof User){
            User user = (User) o;
            if(this.age!=user.age)
                return this.age-user.age;
            else
                return this.name.compareTo(user.name);
        }else
            throw new RuntimeException("类型不匹配");
    }
}
